;
(function(factory) {
	layui.use(['layer', 'laypage', 'table', 'form','tree'], function() {
		factory(jQuery, window, document, layui);
	});
}(function($, window, document, layui) {
	$(function() {
		funOp.init();
	});

	var funOp = {
		limit: 10,
		init: function() {

			$('#seach_submit').click(function() {
				funOp.queryRole(1);
				return false;
			});
			
			funOp.tableInit();
		},

		tableInit: function() {
			layui.table.render({
				elem: '#menu_table',
				limit: funOp.limit,
				id: 'menu_table',
				cols: [
					[{
						field: 'busiSerial',
						title: '交易流水号',
						width: 120
					}, {
						field: 'transDate',
						title: '交易日期',
						width: 100
					}, {
						field: 'transTime',
						title: '交易时间',
						width: 100
					}, {
						field: 'city',
						title: '地区',
						width: 150
					}, {
						field: 'userNo',
						title: '用户号',
						width: 100
					}, {
						field: 'amt',
						title: '缴费金额',
						width: 100
					}, {
						field: 'cardNo',
						title: '缴费账号',
						width: 100
					}, {
						field: 'transType',
						title: '交易状态',
						width: 100
					}, {
						field: 'checkType',
						title: '对账类型',
						width: 100
					}, {
						field: 'remark',
						title: '备注',
						width: 100
					}]
				],
				data: []
			});
		},

		tableReload: function(pageIndex, total, data) {
			layui.table.reload('menu_table', {
				data: data
			});
			layui.laypage.render({
				elem: 'menu_page',
				limit: funOp.limit,
				curr: pageIndex,
				groups: 2,
				count: total,
				layout: ['count', 'prev', 'page', 'next'],
				jump: function(obj, first) {
					if (!first) {
						funOp.queryRole(obj.curr);
					}
				}
			});
		},

		queryRole: function(pageIndex) {
			var params = layui.form.val('seach_form');
			var where = ' where 0=0 ';
			var paramList = [];
			var pageParamList = [];
			$.each(params, function(key, value) {
				if(!value || value == ''){
					return true;
				}
				where += ' and ' + key + ' like concat("%", ? ,"%")';
				paramList.push(value);
			});
			pageParamList = paramList.concat([(pageIndex - 1) * funOp.limit, (pageIndex) * funOp.limit]);
			
			var c = 'SELECT count(0) num from jsdf_translist' + where;
			var s = 'SELECT * from jsdf_translist ' + where + ' order by trans_date desc,trans_time desc limit !,! ';
			
			var cObj = {
				sql: $.fn.commUtil.getSqlStr(c, paramList)
			};
			var sObj = {
				sql: $.fn.commUtil.getSqlStr(s, pageParamList)
			};
			$.newAjax({
				url: $.fn.commUtil.constant.baseUrl + '/dealSql',
				data: JSON.stringify(cObj),
				success: function(res) {
					// console.info(res)
					if (res.code != 0 || res.data.length == 0 || res.data[0] == 0) {
						alert(res.msg);
						return;
					}
					var total = res.data[0].num;
					$.newAjax({
						url: $.fn.commUtil.constant.baseUrl + '/dealSql',
						data: JSON.stringify(sObj),
						success: function(res) {
							// console.info(res)
							if (res.code != 0) {
								alert(res.msg);
								return;
							}
							funOp.tableReload(pageIndex, total, res.data);
						}
					});
				}
			});
		}
	};
}));
